package com.shop.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.shop.common.core.utils.DateUtils;
import com.shop.common.security.utils.SecurityUtils;
import com.shop.product.domain.MallProductAttrInfoModel;
import com.shop.product.domain.MallProductRuleAttrInfo;
import com.shop.product.domain.MallProductRuleInfo;
import com.shop.product.mapper.MallProductRuleInfoMapper;
import com.shop.product.service.MallProductRuleAttrInfoService;
import com.shop.product.service.MallProductRuleInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class MallProductRuleInfoServiceImpl implements MallProductRuleInfoService {
    @Autowired
    MallProductRuleInfoMapper mallProductRuleInfoMapper;
    @Autowired
    MallProductRuleAttrInfoService mallProductRuleAttrInfoService;
    public static final String UPDATE_FLAG="update";
    public static final String INSERT_FLAG="insert";

    /**
     * 添加商品规则
     * @param mallProductRuleInfo
     * @return
     */
    @Transactional
    @Override
    public int insertMallProductRuleInfo(MallProductRuleInfo mallProductRuleInfo) {
        Long userid = SecurityUtils.getLoginUser().getUserid();
        mallProductRuleInfo.setCreateBy(String.valueOf(userid));
        mallProductRuleInfo.setCreateTime(DateUtils.getNowDate());
        mallProductRuleInfo.setUpdateTime(DateUtils.getNowDate());
        int i = mallProductRuleInfoMapper.insertMallProductRuleInfo(mallProductRuleInfo);
        extracted(mallProductRuleInfo, i ,INSERT_FLAG);
        return i;
    }

    @Override
    public List<MallProductRuleInfo> selectMallProductRuleInfoList(MallProductRuleInfo mallProductRuleInfo) {

        return mallProductRuleInfoMapper.selectMallProductRuleInfoList(mallProductRuleInfo);
    }

    private void extracted(MallProductRuleInfo mallProductRuleInfo, int i, String flag) {
        // 如果影响的行数大于0，表示操作成功
        if (i > 0) {
            Long ruleId = mallProductRuleInfo.getId(); // 获取规则ID
            ArrayList<MallProductRuleAttrInfo> ruleAttrInfoList = new ArrayList<>(); // 创建规则属性信息列表
            String ruleAttr = mallProductRuleInfo.getRuleAttr(); // 获取规则属性的JSON字符串
            List<MallProductAttrInfoModel> mallProductAttrInfoModels = JSON.parseArray(ruleAttr, MallProductAttrInfoModel.class); // 将JSON字符串转换为对象列表

            // 如果规则属性对象列表不为空，则处理每个属性对象
            if (CollectionUtils.isNotEmpty(mallProductAttrInfoModels)) {
                for (MallProductAttrInfoModel mallProductAttrInfoModel : mallProductAttrInfoModels) {
                    List<String> ruleAttrList = mallProductAttrInfoModel.getRuleAttrList(); // 获取属性值列表
                    for (String ruleAttrString : ruleAttrList) {
                        MallProductRuleAttrInfo mallProductRuleAttrInfo = new MallProductRuleAttrInfo();
                        mallProductRuleAttrInfo.setAttrValue(ruleAttrString); // 设置属性值
                        mallProductRuleAttrInfo.setRuleId(ruleId); // 设置规则ID
                        ruleAttrInfoList.add(mallProductRuleAttrInfo); // 将设置好的规则属性信息对象添加到列表中
                    }
                }
                // 根据标志判断是否需要更新规则属性信息
                if (UPDATE_FLAG.equals(flag)) {
                    mallProductRuleAttrInfoService.deleteMallproductRuleAttrInfoByRuleId(ruleId); // 删除旧的规则属性信息
                }
                mallProductRuleAttrInfoService.insertMallProductRuleAttrInfoList(ruleAttrInfoList); // 插入新的规则属性信息
            }
        }
    }


}
